/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package json;

/**
 *
 * @author User
 */

import engine.Singleton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import json.Protocol;
import org.json.simple.parser.ParseException;
public class MockClient extends Thread
{
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private String str;
    
   
    
    public MockClient() throws IOException 
    {
        socket = new Socket("127.0.0.1", 8888);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                .getOutputStream())), true);
start();
    }

    
    public void sendUser(String send)
    {
       out.println(send);
    }
    
    public String getMessage()
    {
      String res = str;
      return res;
    }

    public void run()
    {
        try
        {
            while (true) {
            str = in.readLine();
            System.out.println("Mock client comming: " + str);
           Res.getStr(str);

            if (str.equals("{\"Type\": \"End\"}")) {
              out.println("{\"Type\": \"End\"}");
              break;
            }
            if (str.length() > 1) {
              str = "";
            }
          }
          System.out.println("closing...");
        }
        catch (IOException e)
        {
            System.err.println("IO Exception");
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch (IOException e)
            {
                System.err.println("Socket not closed");
            }
        }
    }
    
    
}